const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    traineePhone: null,
    bodyMeasurements: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const traineePhone = wx.getStorageSync("traineePhone");
    const bodyMeasurements = app.globalData.bodyMeasurements;
    this.setData({
      traineePhone
    });
    const that = this;
    const url = app.globalData.url;
    wx.request({
      url: url + '/train/bodyCircumference/' + traineePhone,
      success: function(response) {
        if (response.data.success) {
          const bodyObj = response.data.content;
          bodyMeasurements.forEach(function(item) {
            switch (item.columnName) {
              case 'xiongwei':
                item.value = bodyObj.xiongwei.toFixed(2) || 0;
                break;
              case 'yaowei':
                item.value = bodyObj.yaowei.toFixed(2) || 0;
                break;
              case 'tunwei':
                item.value = bodyObj.tunwei.toFixed(2) || 0;
                break;
              case 'zuodatui':
                item.value = bodyObj.zuodatui.toFixed(2) || 0;
                break;
              case 'youdatui':
                item.value = bodyObj.youdatui.toFixed(2) || 0;
                break;
              case 'zuobi':
                item.value = bodyObj.zuobi.toFixed(2) || 0;
                break;
              case 'youbi':
                item.value = bodyObj.youbi.toFixed(2) || 0;
                break;
              case 'jiankuan':
                item.value = bodyObj.jiankuan.toFixed(2) || 0;
                break;
            }
          });
          that.setData({
            bodyMeasurements
          });
        }
      }
    })
  },
  inputValue: function(e) {
    const index = e.currentTarget.dataset.index;
    const bodyMeasurements = this.data.bodyMeasurements;
    bodyMeasurements[index].value = e.detail.value;
    this.setData({
      bodyMeasurements
    });
  },
  save: function() {
    const bodyMeasurements = this.data.bodyMeasurements;
    const phone = this.data.traineePhone;
    const url = app.globalData.url;
    let xiongwei = 0;
    let yaowei = 0;
    let tunwei = 0;
    let zuodatui = 0;
    let youdatui = 0;
    let zuobi = 0;
    let youbi = 0;
    let jiankuan = 0;
    bodyMeasurements.forEach(function(item) {
      switch (item.columnName) {
        case 'xiongwei':
          xiongwei = item.value;
          break;
        case 'yaowei':
          yaowei = item.value;
          break;
        case 'tunwei':
          tunwei = item.value;
          break;
        case 'zuodatui':
          zuodatui = item.value;
          break;
        case 'youdatui':
          youdatui = item.value;
          break;
        case 'zuobi':
          zuobi = item.value;
          break;
        case 'youbi':
          youbi = item.value;
          break;
        case 'jiankuan':
          jiankuan = item.value;
          break;
      }
    });
    wx.request({
      url: url + '/train/saveBodyCircumference',
      data: {
        phone,
        xiongwei,
        yaowei,
        tunwei,
        zuodatui,
        youdatui,
        zuobi,
        youbi,
        jiankuan
      },
      method: 'POST',
      success: function(response) {
        if (response.data.success) {
          wx.navigateBack({
            delta: 1
          });
          wx.showToast({
            title: '保存成功',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '保存失败',
            icon: 'none'
          })
        }
      }
    })
  }
})